<template>
  <scroll class="home-scroll" :pullup="true" @scrollToEnd="loadMore" :probeType="3" ref="scroll" :listenScroll="true" @scroll="scroll">
    <div class="category">
      <!-- 二级分类列表 -->
      <div class="category-list-wrapper">
        <ul class="category-list">
          <li class="category-item" :class="{row2:index>4}" v-for="(item,index) in categoryItem.children" @click="tocatgoods(item)">
            <div class="item-image">
              <img :src="item.image_url">
            </div>
            <div class="item-name">
              <span>{{item.opt_name}}</span>
            </div>
          </li>
          <li class="category-item row2" @click="tocatgoods(categoryItem)">
            <div class="item-image">
              <img style="width: .22rem;height: .22rem;" src="" alt="">
            </div>
            <div class="item-name">
              <span>查看全部</span>
            </div>
          </li>
        </ul>
      </div>
      <!-- 商品列表 -->
      <div class="product-list-wrapper">
        <product-list :hasMore="hasMore" :goodsList="goodsList" v-if="goodsList"></product-list>
        <loading v-else></loading>
      </div>
    </div>
    <go-top @toTop="toTop" :scrollY="scrollY"></go-top>
  </scroll>
</template>

<script>
import productList from "../../../components/ProductList/productList";
import loading from "../../../components/loading/loading";
import { cateproductList } from "../../../api/getDatas";
import Scroll from "../../../common/scroll";
import goTop from "../../../components/goTop/goTop";
export default {
  components: {
    productList,
    loading,
    Scroll,
    goTop
  },
  computed: {
    categoryItem() {
      return this.$store.state.cateHeader.categoryItem;
    }
  },
  data() {
    return {
      goodsList: null,
      opt_type: 1,
      size: 100,
      offset: 0,
      hasMore: true,
      scrollY: null
    };
  },

  methods: {
    tocatgoods(item) {
      const type = item.children ? 1 : 2;
      this.$router.push({
        path: "/catgoods",
        query: {
          opt_id: item.opt_id,
          opt_type: type,
          opt_name: item.opt_name
        }
      });
    },
    toTop() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    scroll(pos) {
      this.scrollY = -pos.y;
    },
    loadMore() {
      let offset = (this.offset += this.size);
      cateproductList(
        this.categoryItem.opt_id,
        this.opt_type,
        this.size,
        offset
      )
        .then(res => {
          this.goodsList = this.goodsList.concat(res.data.goods_list);
        })
        .catch(err => {
          this.loadMore();
        });
    },
    initData() {
      cateproductList(
        this.categoryItem.opt_id,
        this.opt_type,
        this.size,
        this.offset
      )
        .then(res => {
          this.goodsList = res.data.goods_list;
        })
        .catch(err => {
          this.initData();
        });
    }
  },
  mounted() {
    this.initData();
  }
};
</script>

<style lang="less" scoped>
.home-scroll {
  height: 100%;
  overflow: hidden;
}
.category-list-wrapper {
  width: 100%;
  height: 1.7rem;
  padding: 0.05rem 0.06rem;
  position: relative;
  background-color: #fff;
  margin-bottom: 0.08rem;
  .category-list {
    display: flex;
    flex-wrap: wrap;
    .row2 {
      margin-top: 0.13rem;
    }
    .category-item {
      .item-name {
        width: 100%;
        text-align: center;
        height: 0.17rem;
        line-height: 0.18rem;
        font-size: 0.12rem;
        color: #666;
      }
      .item-image {
        height: 0.53rem;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      width: 20%;
      img {
        width: 0.54rem;
        height: 0.54rem;
      }
      .no-img {
        width: 0.54rem;
        height: 0.54rem;
        border-radius: 50%;
        background-color: #f1f1f1;
      }
    }
  }
}
</style>

